<!-- 重命名组件 -->
<template>
  <!-- 触发模态框的链接 -->
  <a href="#" @click.prevent="showModal = true">
    <i class="bi bi-input-cursor"></i>
  </a>

  <!-- 模态框 -->
  <transition name="modal-fade">
    <div class="modal" v-if="showModal">
      <div class="modal-content">
        <div class="d-flex justify-content-between align-items-center">
          <h2>重命名</h2>
          <span class="close" @click="showModal = false">&times;</span>
        </div>
        <form @submit.prevent="renameItem">
          <input
            type="text"
            v-model="newName"
            placeholder="输入新名称"
            required
          />
          <button type="submit">重命名</button>
        </form>
      </div>
    </div>
  </transition>
</template>

<script>
import axios from "axios"; // 确保你已经安装了axios库

export default {
  props: ["fileName", "itemType", "itemId"],
  emits: ["itemRenamed"],
  data() {
    return {
      showModal: false,
      newName: this.fileName,
    };
  },
  methods: {
    async renameItem() {
      try {
        // 发送POST请求到Laravel控制器
        const response = await axios.post("/api/rename/renameFileOrFolder", {
          id: this.itemId,
          type: this.itemType,
          new_name: this.newName,
        });

        // 处理响应
        if (response.data.success) {
          this.$emit("itemRenamed");
          alert(response.data.success);
          this.showModal = false;
        } else {
          alert("重命名失败：" + response.data.error);
        }
      } catch (error) {
        console.error(error);
        alert("重命名失败");
      }
    },
  },
};
</script>

<style scoped>
/* 添加过渡效果的CSS样式 */
.modal-fade-enter-active,
.modal-fade-leave-active {
  transition: opacity 0.5s, transform 0.5s;
}
.modal-fade-enter,
.modal-fade-leave-to {
  opacity: 0;
  transform: scale(0.95);
}
/* Modal background overlay */
.modal {
  position: fixed; /* Stay in place */
  z-index: 1000; /* Sit on top */
  left: 0;
  top: 0;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgba(0, 0, 0, 0.5); /* Black background with opacity */
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

/* Modal content */
.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 50%; /* Could be more or less, depending on screen size */
  max-width: 600px; /* Maximum width */
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  border-radius: 8px; /* Rounded border */
}

/* Modal header */
.modal-content h2 {
  margin: 0;
  text-align: center;
  font-size: 20px;
  color: #333;
}

/* Modal close button */
.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
  cursor: pointer;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
}

/* Modal form */
.modal-content form {
  display: flex;
  flex-direction: column;
  margin-top: 20px;
}

.modal-content input[type="text"] {
  padding: 10px;
  margin-bottom: 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: 16px;
}

.modal-content button[type="submit"] {
  background-color: #4caf50; /* Green */
  border: none;
  color: white;
  padding: 10px 20px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  border-radius: 4px;
  cursor: pointer;
}

.modal-content button[type="submit"]:hover {
  background-color: #45a049;
}
</style>
